% Generalized Gaussian Distribution
% CDF (version 2 in: https://en.wikipedia.org/wiki/Generalized_normal_distribution)
% Location (real)
% Scale (>0)
% Shape (real, but restrict it between -1 and 1 for reasonable outcomes,
% and check result not at the boundary)
% also high values get log() to complex numbers
function[tx]=ggd_cdf(x,theta)
    location=theta(1);
    scale=theta(2);
    shape=theta(3);
    try
         normdb = makedist('norm',0,1);
         if shape == 0
             y  = (x-location)./scale;
             tx = cdf(normdb,y);
         elseif shape > 0
             if x<= location+(scale/shape)
                y = -(1./shape).*log(1- ((shape.*(x-location))./scale));
                tx = cdf(normdb,y);
             else
                tx = 1;
             end
         else %shape<0
             if x>= location+(scale/shape)
                 y = -(1./shape).*log(1- ((shape.*(x-location))./scale));
                 tx = cdf(normdb,y);
             else
                 tx = 0;
             end
         end

    catch
         tx = 2;%i.e., clearly wrong

    end
     tx=ones(size(tx)).*tx;%necessary otherwise integrate() based on this CDF requires ArrayValued=TRUE and that is crazy slow
end
        